/**
 * 
 */
package com.google.gwt.sample.stockwatcher.server;

import com.google.gwt.sample.stockwatcher.client.DelistedException;
import com.google.gwt.sample.stockwatcher.client.LoginService;
import com.google.gwt.user.client.Cookies;
import com.google.gwt.user.server.rpc.RemoteServiceServlet;
import com.google.gwt.http.client.*;

/**
 * @author th3 ied kid
 *
 */
public class LoginServiceImplementation extends RemoteServiceServlet implements
		LoginService {

	
	/**
	 * 
	 */
	private static final long serialVersionUID = -3861138612106597273L;
	private RequestBuilder rbs = null;
	
	
	@Override
	public String validPass(String uname, String pass) throws DelistedException {
String url ="https://www.google.com/accounts/AuthSubRequest?next=http%3A%2F%2Flocalhost%3A8888&scope=http%3A%2F%2Fwww.google.com%2Fcalendar%2Ffeeds%2F%20http%3A%2F%2Fpicasaweb.google.com%2Fdata&session=0&secure=0";
		if(rbs==null)
		{
			rbs=  new RequestBuilder(RequestBuilder.GET,url);		
		}
		if(rbs==null)
			return "Invalid eroror rbs is empty";

		try {
			RequestCalli rqC= new RequestCalli();
			Request rqt = rbs.sendRequest(null,rqC );
			if(rqC.status!=null)
				if(rqC.status.equals("Error"))
					return "Error";
			if(rqC.oResponse!=null)
			{
				Cookies.setCookie("token",rqC.oResponse.getHeader("token"));
				
				
				return rqC.oResponse.getHeader("token");
			}
		} catch (RequestException e) {
			// TODO Auto-generated catch block
			return "request Exception";
		}
		
		return "Unknown Error";
	}



}
